package com.jiawa.train.member.controller;

import com.jiawa.train.common.resp.CommonResp;
import com.jiawa.train.member.req.MemberLoginReq;
import com.jiawa.train.member.req.MemberRegisterReq;
import com.jiawa.train.member.req.MemberSendCodeReq;
import com.jiawa.train.member.resp.MemberLoginResp;
import com.jiawa.train.member.service.MemberService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName TestController
 * @Description
 * @Author GateOfRust
 * @Date 2024/5/15 下午2:19
 * @Version 1.0
 */


@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @GetMapping("/count")
    public CommonResp<Integer> hello() {
//        CommonResp<Integer> commonResp = new CommonResp<>();
//        Integer count = memberService.count();
//        commonResp.setContent(count);

        //可以简写为：因为存在构造方法
        return new CommonResp<>(memberService.count());
    }

    @PostMapping("/register")
    public CommonResp<Long> register(@Valid MemberRegisterReq req) {
//        CommonResp<Long> commonResp = new CommonResp<>();
//        Long register = memberService.register(req);
//        commonResp.setContent(register);
        return new CommonResp<>(memberService.register(req));
    }

    @PostMapping("/send-code")
    public CommonResp<Long> SendCode(@Valid @RequestBody MemberSendCodeReq req) {
        memberService.sendCode(req);
        return new CommonResp<>();
    }

    @PostMapping("/login")
    public CommonResp<MemberLoginResp> Login(@Valid @RequestBody MemberLoginReq req) {
        MemberLoginResp login = memberService.Login(req);
        return new CommonResp<>(login);
    }
}
